﻿/*
   绑定视图：UserView.xaml
   文件说明：控制用户管理业务层
*/
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using ZFSData;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Common.CoreLib.Expressions;
using ZFSDomain.Common.CoreLib.Helper;
using ZFSDomain.Interface.User;
using ZFSDomain.Service;
using ZFSDomain.SysModule;
using ZFSInterface.User;

namespace ZFSDomain.ViewModel.User
{

    public class UserViewModel : BaseOperation<tb_User>
    {

        public override void GetPageData(int pageIndex)
        {
            try
            {
                IUser user = ZFSBridge.BridgeFactory.GetUserBridge();

                var where = ExpressionHelper.GenerateQueryExp(new tb_User() { Account = SearchText, UserName = SearchText });

                var UserList = user.GetPagingModels(pageIndex, PageSize, out int total,
                   where, u => u.isid);
                TotalCount = total;
                GridModelList.Clear();
                UserList.ForEach((arg) => GridModelList.Add(arg));
                base.SetPageCount();

            }
            catch (Exception ex)
            {
                Msg.Error(ex.Message);
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        public override void Add<T>(T obj)
        {
            UserDialogViewModel model = new UserDialogViewModel();
            var dialog = ServiceProvider.Instance.Get<IUserDialog>();
            dialog.BindViewModel(model);
            var result = dialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                try
                {
                    ZFSInterface.User.IUser userSerivce = ZFSBridge.BridgeFactory.GetUserBridge();
                    model.User.IsLocked = 0;
                    model.User.FlagAdmin = "0";
                    model.User.FlagOnline = "0";
                    model.User.LoginCounter = 0;
                    model.User.CreateTime = DateTime.Now;

                    tb_User r = userSerivce.AddEntity(model.User);
                    if (r != null)
                    {
                        GridModelList.Add(r);
                        Msg.Info("添加成功!");
                    }
                }
                catch (Exception ex)
                {
                    Msg.Error(ex.Message);
                }
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public override void Edit<T>(T CurrentUser)
        {
            if (!this.GetButtonAuth(Authority.EDIT)) return;

            if (CurrentUser != null)
            {
                UserDialogViewModel view = new UserDialogViewModel();

                view.User = ZFSBridge.BridgeFactory.GetUserBridge().GetModelByAccount((CurrentUser as tb_User).Account);

                var dialog = ServiceProvider.Instance.Get<IUserDialog>();
                dialog.BindViewModel(view);
                var result = dialog.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    ZFSInterface.User.IUser userSerivce = ZFSBridge.BridgeFactory.GetUserBridge();
                    bool r = userSerivce.UpdateEntity(view.User);
                    if (r)
                    {
                        var mod = GridModelList.FirstOrDefault(t => t.isid.Equals(view.User.isid));
                        GridModelList.Remove(mod);
                        GridModelList.Add(view.User);
                        Msg.Info("更新成功!");
                    }
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        public override void Del<TModel>(TModel CurrentUser)
        {
            if (!this.GetButtonAuth(Authority.DELETE)) return;

            if (CurrentUser != null)
            {
                var model = CurrentUser as tb_User;

                bool result = Msg.Question(string.Format("确认删除用户：{0}", model.UserName));

                try
                {
                    if (result)
                    {
                        ZFSInterface.User.IUser userSerivce = ZFSBridge.BridgeFactory.GetUserBridge();
                        bool r = userSerivce.DeleteEntity(model);
                        if (r)
                        {
                            var mod = GridModelList.FirstOrDefault(t => t.isid.Equals(model.isid));
                            GridModelList.Remove(mod);
                            Msg.Info("删除成功!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Msg.Error(ex.Message);
                }
            }
        }
    }
}
